<?php
/**
 * Prado Portal.
 *
 * @author Steen Rabol <steen.rabol@gmail.com>
 * @link http://www.pradoportal.dk/
 * @copyright Copyright &copy; 2006,2007,2008 Steen Rabol
 * @license http://www.pradoportal.dk
 * @version $Id: EditUser.php 469 2011-01-25 17:25:40Z steen.rabol $
 *
 */

class EditUser extends PortalSystemPage
{
	private $_userRecord = null;

	public function onInit($param)
	{
		if($this->User->IsGuest)
		{
			$this->reportError(1,"You do not have permission to view this page");
		}

		if($this->Request['id'] == 0 || $this->Request['id'] === '0')
		{
			$this->reportError(1,Prado::localize('profile_id_invalid',array($id)));
		}

		parent::onInit($param);

		if(($id = TPropertyValue::ensureInteger($this->Request['id'])) !== null)
		{
			if($this->User->RoleId != $this->Application->Parameters['AdminRole'] && $this->User->Id !== $id)
			{
				$this->reportError(500,Prado::localize('profile_edit_disallowed',array($id)));
			}
		}
		else
		{
			$id	= $this->User->Id;
		}

		if(($this->_userRecord = $this->DataAccess->createCommand("select * from tblusers where id=$id")->queryRow()) === null)
		{
			$this->reportError(1,Prado::localize('profile_id_invalid',array($id)));
		}
	}

	public function onLoad($param)
	{
		parent::onLoad($param);
		$this->Title = $this->Application->Parameters['SiteTitle'] . " - " . Prado::localize("Edit profile");
		if(!$this->IsPostBack)
		{
			$userRecord					= $this->_userRecord;
			$this->Username->Text		= $userRecord['name'];
			$this->FullName->Text		= $userRecord['full_name'];
			$this->Email->Text			= $userRecord['email'];
			$this->SiteNews->Checked	= TPropertyValue::ensureBoolean($userRecord['sitenews']);

			$this->dlLanglist->PromptText		= Prado::localize("Select");
			$this->dlLanglist->PromptValue		= 0;
			$this->dlLanglist->DataSource		= $this->DataAccess->createCommand("select culture,name from tbllanguages where active=1")->query();
			$this->dlLanglist->SelectedValue	= $userRecord['culture'];

			$this->dlLanglist->dataBind();
		}
	}

	public function saveButtonClicked($sender,$param)
	{
		if($this->IsValid)
		{
			$userRecord = $this->_userRecord;
			if($this->Password->Text !=='')
			{
				$userRecord['password']	= hash("sha256",$this->Password->Text);
			}

			$userRecord['full_name']	= $this->FullName->Text;
			$userRecord['email']		= $this->Email->Text;
			$userRecord['sitenews']		= TPropertyValue::ensureInteger($this->SiteNews->Checked);
			$userRecord['culture']		= $this->dlLanglist->SelectedValue;
			$this->DataAccess->createCommand("update tblusers set password='" . $userRecord['password'] . "',full_name='" . $userRecord['full_name'] . "',email='". $userRecord['email'] . "',sitenews='" . $userRecord['sitenews'] . "',culture='" . $userRecord['culture'] . "' where id=" . $userRecord['id'])->execute();
			$this->gotoPage('System.ViewUser',array('id'=> $userRecord['id']));
		}
	}
}
?>